﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

namespace CommerceForUmbraco.usercontrols.usercontrols.commerce
{
    public partial class PWSearchBox : System.Web.UI.UserControl
    {

        // The umbraco macro caller needs to pass this in...
        public string SearchRedirectURL
        {
           // NOTE: not being used at this time...not working???  See search.aspx redirect below
            get;
            set;
        }
        
        protected void Page_Load(object sender, EventArgs e)
        {
            // Enter handler...
            if (this.IsPostBack == false)
            {
                // this.ctrlSearchInputTextBox.Attributes.Add("onkeypress", "if(event.which || event.keyCode){if ((event.which == 13) || (event.keyCode == 13)) {document.getElementById('" + this.ctrlDoSearchBtn.UniqueID + "').click();return false;}} else {return true}; ");
                this.ctrlSearchInputTextBox.Attributes.Add("onKeyPress", "javascript:if (event.keyCode == 13) __doPostBack('" + ctrlDoSearchBtn.UniqueID + "','')");
            }
            
            if (this.IsPostBack == false &&
                string.IsNullOrEmpty(Request["q"]) == false)
            {
                // restore the previously posted query...
                this.ctrlSearchInputTextBox.Text = Request["q"];
            }
        }

        protected void ctrlDoSearchBtn_Click(object sender, EventArgs e)
        {
            string redirectWithQuery = String.Format("{0}?q={1}", SearchRedirectURL, this.ctrlSearchInputTextBox.Text);
            Response.Redirect(redirectWithQuery, true);
        }
    }
}